package com.syc.test;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

@Slf4j
public class DateUtils {

    private DateUtils(){}

    public static final String ymdstr = "yyyy-MM-dd HH:mm:ss";

    public static final String YMDstr = "yyyy-MM-dd";

    public static Date parse (SimpleDateFormat sdf, String date) {
        try {
            return sdf.parse(date);
        } catch (Exception e) {
            return null;
        }
    }


    public static String coverUTCDate(String text){
        Date date = parse(text);
        return coverUTCDate(date);
    }

    public static String coverUTCDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,-8);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return sdf.format(calendar.getTime());
    }


    public static Date parseUTCText(String text) throws ParseException {
        if(StrUtil.isEmpty(text)){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        if (text.indexOf(".") > -1) {
            String prefix = text.substring(0, text.indexOf("."));
            String suffix = text.substring(text.indexOf("."));
            if (suffix.length() >= 5) {
                suffix = suffix.substring(0, 4) + "Z";
            } else {
                int len = 5 - suffix.length();
                StringBuilder temp = new StringBuilder();
                temp.append(suffix, 0, suffix.length() - 1);
                for (int i = 0; i < len; i++) {
                    temp.append("0");
                }
                suffix = temp.append("Z").toString();
            }
            text = prefix + suffix;
        } else {
            text = text.substring(0, text.length() - 1) + ".000Z";
        }
        return sdf.parse(text);
    }

    public static Date parse(String date){
        try {
            SimpleDateFormat sdf_ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf_ymdhms.parse(date);
        } catch (Exception e) {
            log.error("日期转换失败",e);
        }
        return null;
    }

    public static Date parseYMD(String date){
        try {
            SimpleDateFormat sdf_ymdhms = new SimpleDateFormat("yyyy-MM-dd");
            return sdf_ymdhms.parse(date);
        } catch (Exception e) {
            log.error("日期转换失败",e);
        }
        return null;
    }

    public static String format (String pattern, Date date) {
        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 格式化日期 yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String formatDate (Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(ymdstr);
        return sdf.format(date);
    }

    /**
     * 格式化日期 yyyy-MM-dd
     * @param date
     * @return
     */
    public static String formatYMDDate (Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(YMDstr);
        return sdf.format(date);
    }

    public static String formatWeekCN(Date date){
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        return dateFm.format(date);
    }

    public static int getWeekNumber(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    public static LocalDateTime parseLocalDateTime(String dateStr){
        if(dateStr==null){
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateStr, formatter);
    }

    public static LocalDate parseLocalDate(String dateStr){
        if(dateStr==null){
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(dateStr, formatter);
    }

    public static String formatLocalDateTime(LocalDateTime localDateTime){
        if(localDateTime==null){
            return "";
        }
        DateTimeFormatter dtf_ymdhms=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf_ymdhms.format(localDateTime);
    }

    public static String formatLocalDate(LocalDate localDate){
        if(localDate==null){
            return "";
        }
        DateTimeFormatter dtf_ymd=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return dtf_ymd.format(localDate);
    }

    public static String formatUTCDate(String date){
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat sdf=new SimpleDateFormat(ymdstr);
            return sdf.format(parseUTCText(date));
        } catch (Exception e) {
            log.info("时间转换失败");
        }
        return null;
    }

    public static String formatUSDate(String date){
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat sdf=new SimpleDateFormat(ymdstr);
            SimpleDateFormat us_sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
            return sdf.format(us_sdf.parse(date));
        } catch (Exception e) {
            log.info("时间转换失败");
        }
        return null;
    }


    /**
     * yyyy-MM-dd
     * @return
     */
    public static String formatDate () {
        SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
        return ymd.format(new Date());
    }

    /**
     * 判断与今天是否是同一天
     * @return
     */
    public static boolean isSameDate (Date timeIdentify) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (timeIdentify != null) {
            String time = sdf.format(timeIdentify);
            String curDate = sdf.format(new Date());
            if (curDate.equals(time)) {
                return true;
            }
        }
        return false;
    }

    public static boolean range(Date beginDate,Date endDate,Date targerDate){
        if(targerDate.getTime()>beginDate.getTime() && targerDate.getTime()<endDate.getTime()){
            return true;
        }
        return false;
    }

    /**
     * 日期加减秒
     * @param date
     * @return
     */
    public static Date opSecond (Date date, int second) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        //日期加60秒天
        rightNow.add(Calendar.SECOND,second);
        return rightNow.getTime();
    }


    /**
     * 日期加减
     * @param date
     * @return
     */
    public static Date opTime (Date date, int time,int timeUnit) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(timeUnit,time);
        return rightNow.getTime();
    }

    /**
     * 两个日期相差>diff,return true
     * @param start
     * @param end
     * @param diff 设置的秒数
     * @return
     */
    public static boolean diffDate (Date start, Date end, int diff) {
        return Math.abs(diffDateTime(start,end))/1000 >= diff;
    }

    public static long diffDateTime(Date start,Date end){
        long startTime = start.getTime();
        long endTime = end.getTime();
        return (endTime - startTime);
    }

    /**
     * 两个日期相差<diff,return true
     * @param start
     * @param end
     * @param diff 设置的秒数
     * @return
     */
    public static boolean diffDate2 (Date start, Date end, int diff) {
        return Math.abs(diffDateTime(start,end))/1000 <= diff;
    }


    public static Date changge(LocalDateTime localDateTime){
        Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        return date;
    }

    public static LocalDateTime parseHMS(String hmsTime) {
        String[] hms = hmsTime.split(":");
        LocalDateTime now = LocalDateTime.now();
        return now.withHour(Integer.parseInt(hms[0])).withMinute(Integer.parseInt(hms[1])).withSecond(Integer.parseInt(hms[2]));
    }

    //0-日,1-周,2-月,3-年
    public static String getDateTimesByType(Integer type) {
        LocalDateTime now = LocalDateTime.now();
        if(type.equals(0)){
            return String.format("%s,%s", formatLocalDateTime(now.withHour(0).withMinute(0).withSecond(0)),formatLocalDateTime(now));
        }else if(type.equals(1)){
            return String.format("%s,%s", formatLocalDateTime(now.minusDays(7)),formatLocalDateTime(now));
        }else if(type.equals(2)){
            return String.format("%s,%s", formatLocalDateTime(now.minusMonths(1)),formatLocalDateTime(now));
        }else if(type.equals(3)){
            return String.format("%s,%s", formatLocalDateTime(now.minusYears(1)),formatLocalDateTime(now));
        }
        return null;
    }
}
